--------------------------------------------------
-- 超值基金

-- db.superFund[type] = {ts, loginDay, sendDay}
--------------------------------------------------

local PresentExcel = require("excel.present")
local MailExcel = require("excel.mail")
local DB = require("common.DB")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Grid = require("bag.Grid")
local MailManager = require("mail.MailManager")
local ItemDefine = require("bag.ItemDefine")
local BuyLogic = require("topup.BuyLogic")
local Broadcast = require("broadcast.Broadcast")
local BagLogic = require("bag.BagLogic")
local PanelDefine = require("broadcast.PanelDefine")
local YunYingLogic = require("yunying.YunYingLogic")
local SceneHandler = require("scene.Handler")

-- 超值基金类型
local TYPE_SMALL = 1
local TYPE_BIG = 2


-- 状态
local STATE_CANT_BUY = 0 
local STATE_CAN_BUY = 1
local STATE_HAD_BUY = 2

local MAILID_LIST = {61, 60}

-- 获取状态
local function getBuyState(human, fundType)
    if human.db.superFund and 
        human.db.superFund[fundType] then
        return STATE_HAD_BUY
    end
    if not human.db.bigPrivFlag then
        return STATE_CANT_BUY
    end
    return STATE_CAN_BUY
end

-- 获取配置
local function getConfig(fundType)
    if fundType == TYPE_BIG then
        return PresentExcel.superBigFund
    end
    if fundType == TYPE_SMALL then
        return PresentExcel.superSmallFund
    end
end

-- 已发送天数 0-30
local function getSendDay(human, fundType)
    if not human.db.superFund or 
        not human.db.superFund[fundType] then
        return 0
    end
    local fundData = human.db.superFund[fundType]
    return fundData.sendDay
end

-- 封装奖励结构体
local function fontRewardNet(net, day, rcf, sendDay)
    net.day = day 
    Grid.makeItem(net.reward, rcf.reward[1][1], rcf.reward[1][2])
    net.isGet = (sendDay >= day) and 1 or 0
end

-- 查询
function superFundQuery(human, fundType)
    local config = getConfig(fundType)
    if not config then return end

    local fdefine = config[1]
    local msgRet = Msg.gc.GC_SUPER_FUND_QUERY
    local net = msgRet.data
    net.type = fundType 
    net.canBuy = getBuyState(human, fundType)
    local sendDay = getSendDay(human, fundType)
    net.list[0] = 0
    for i = 1, #config do
        local rcf = config[i]
        if rcf.preview == 1 then
            net.list[0] = net.list[0] + 1
            fontRewardNet(net.list[net.list[0]], i, rcf, sendDay)
        end
    end
    net.vipexp = fdefine.vipexp
    net.zuanshi = fdefine.zuanshi
    BuyLogic.fontBuyItem(human, net.buyItem, fdefine.buyID)
    --Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

-- 奖励查看
function rewardQuery(human, fundType)
    local config = getConfig(fundType)
    if not config then return end

    local msgRet = Msg.gc.GC_SUPER_FUND_REWARD_QUERY
    msgRet.type = fundType
    local sendDay = getSendDay(human, fundType)
    msgRet.reward[0] = #config
    for i = 1, msgRet.reward[0] do
        fontRewardNet(msgRet.reward[i], i, config[i], sendDay)
    end
    --Msg.trace(msgRet)    
    Msg.send(msgRet,human.fd)
end

-- 购买
function buySuperFund(human, fundType)
    local config = getConfig(fundType)
    if not config then return end
    local fdefine = config[1]

    local fundData = {}
    fundData.ts = os.time() 
    fundData.loginDay = 1
    fundData.sendDay = 0
    human.db.superFund = human.db.superFund or {}
    human.db.superFund[fundType] = fundData
    sendReward(human)
    BagLogic.cleanMomentItemList()
    BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, ItemDefine.ITEM_ZUANSHI_ID, fdefine.zuanshi)
    BagLogic.addMomentItemList(human, "superFund")
    Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
    for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
        break
    end
    superFundQuery(human, fundType)
end

-- 发送奖励
function sendReward(human)
    if not human.db.superFund then return end

    local delList = nil
    local isChange = nil
    for fundType, fundData in pairs(human.db.superFund) do
        local config = getConfig(fundType)
        local sendDay = fundData.sendDay                    
        for day = sendDay + 1, fundData.loginDay do
            local rcf = config and config[day]
            if not rcf then break end
            fundData.sendDay = day                
            local mailID = MAILID_LIST[fundType]
            local mailConfig = MailExcel.mail[mailID]
            local title = mailConfig.title
            local content = Util.format(mailConfig.content, day)
            local senderName = mailConfig.senderName
            MailManager.add(MailManager.SYSTEM, human.db._id, title, content, rcf.reward, senderName)  
            isChange = true            
        end
        if not (config and config[fundData.loginDay]) then
            delList = delList or {}
            delList[#delList + 1] = fundType
        end
    end
    if delList then
        for _, fundType in ipairs(delList) do
            human.db.superFund[fundType] = nil
        end
        if not next(human.db.superFund) then
            human.db.superFund = nil
        end
    end
    -- 发完就存DB，保险
    if isChange or delList then
        ObjHuman.save(human)
    end
end

-- 登录
function onLogin(human)
    updateDaily(human)
    sendReward(human)
end

-- 0点刷新
function updateDaily(human)
    if not human.db.superFund then return end

    local isChange = nil
    for fundType, fundData in pairs(human.db.superFund) do
        if not Util.isSameDay(fundData.ts) then
            fundData.ts = os.time()
            fundData.loginDay = fundData.loginDay + 1
            isChange = true
        end
    end
    if isChange then
        sendReward(human)
    end
end

function isOpen(human)
	if not SceneHandler.canCharge(human) then
		return
	end
	
	return true
end

-- 没购买就红点？？
function isRed(human, YYInfo, funcConfig)
    if funcConfig.panelID == PanelDefine.PANEL_ID_67 then -- 128
        if getBuyState(human, TYPE_SMALL) ~= STATE_HAD_BUY then
            return true
        end
    elseif funcConfig.panelID == PanelDefine.PANEL_ID_68 then --328
        if getBuyState(human, TYPE_BIG) ~= STATE_HAD_BUY then
            return true
        end
    end
end